#include "bsp_display.h"
#include "bsp_param.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"

QueueHandle_t display_led_queue = NULL;
const uint8_t Conversion_Table[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

static void Add_Idle_Time()
{
	/* 空闲位 */
	vTaskDelay(10 / portTICK_PERIOD_MS);
}

static void TM1652_WR(uint8_t uart_type, uint8_t Data)
{
    uart_write_bytes(uart_type, &Data, 1);
}


void uart1_change_tx_pin(gpio_num_t old_tx_pin, gpio_num_t new_tx_pin) {
    // 步骤1：暂停UART通信
    uart_wait_tx_done(UART_NUM_1, pdMS_TO_TICKS(100));

    // 步骤2：解除原引脚功能
    // gpio_reset_pin(old_tx_pin);
    gpio_set_direction(old_tx_pin, GPIO_MODE_INPUT);

    // 步骤3：配置新引脚
    uart_set_pin(UART_NUM_1, new_tx_pin, UART_PIN_NO_CHANGE,
                UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    

}

static void TM1652_Display(uint8_t *display_buffer)
{
    TM1652_WR(UART_NUM_1, LED_TM1652_DISPLAY_GRID1);
    TM1652_WR(UART_NUM_1, display_buffer[GRID1]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID2]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID3]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID4]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID5]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID6]);

    uart1_change_tx_pin(DISPLAY_UART_TX1_PORT, DISPLAY_UART_TX2_PORT);
    TM1652_WR(UART_NUM_1, LED_TM1652_DISPLAY_GRID1);
    TM1652_WR(UART_NUM_1, display_buffer[GRID7]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID8]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID9]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID10]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID11]);
    TM1652_WR(UART_NUM_1, display_buffer[GRID12]);
    Add_Idle_Time();
    TM1652_WR(UART_NUM_1, LED_TM1652_MODESET_CMD);
    TM1652_WR(UART_NUM_1, LED_TM1652_PARAMSET_DATA);
    uart1_change_tx_pin(DISPLAY_UART_TX2_PORT, DISPLAY_UART_TX1_PORT);
    TM1652_WR(UART_NUM_1, LED_TM1652_MODESET_CMD);
    TM1652_WR(UART_NUM_1, LED_TM1652_PARAMSET_DATA);

}

void display_task(void* arg)
{
    uint8_t led_display[GRID_MAX] = {0};
    for(;;){
        if (xQueueReceive(display_led_queue, led_display, portMAX_DELAY)){
            TM1652_Display(led_display);
        }
    }

}

void display_led_start()
{
    uint8_t led_start_buffer[GRID_MAX] = {0};
    memset(led_start_buffer, 0XFF, GRID_MAX);
    TM1652_Display(led_start_buffer);
}


void display_init()
{
    const uart_config_t uart_config = {
        .baud_rate  = 19200,
        .data_bits  = UART_DATA_8_BITS,
        .parity     = UART_PARITY_ODD,
        .stop_bits  = UART_STOP_BITS_1,
        .flow_ctrl  = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
  
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, DISPLAY_UART_TX1_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_1, 1024, 0, 0, NULL, 0);

    display_led_queue = xQueueCreate(5, GRID_MAX);
    xTaskCreate(&display_task, "display_task", 4096, NULL, 10, NULL);
    display_led_start();
}

static uint8_t grid_buffer[GRID_MAX] = {0};
uint8_t *TM1652_led_datatrans(void *display_struct, uint8_t flash_status)
{
    sys_main_param_t *display_param = NULL;
    display_param = display_struct;
    static uint16_t data_strength_old[MODE_MAX] = {0};
 
    static union grid5_t grid5_temporary = {0};
    static union grid6_t grid6_temporary = {0};
    static union grid7_t grid7_temporary = {0};
    static union grid8_t grid8_temporary = {0};
    static union grid9_t grid9_temporary = {0};
    static union grid10_t grid10_temporary = {0};
    static union grid11_t grid11_temporary = {0};
    static union grid12_t grid12_temporary = {0};
    /* 时间 */
    grid_buffer[GRID1] = Conversion_Table[display_param->sys_countdown/60/10];    // 分
    grid_buffer[GRID2] = Conversion_Table[display_param->sys_countdown/60%10];
    grid_buffer[GRID3] = Conversion_Table[display_param->sys_countdown%60/10];    // 秒
    grid_buffer[GRID4] = Conversion_Table[display_param->sys_countdown%60%10];    // 秒

    /* 蓝牙 */
    if(display_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
        grid5_temporary.grid5_bit_t.grid5_ble = 1;
    }else{
        grid5_temporary.grid5_bit_t.grid5_ble = 0;
    }
    
    /* wifi */
    if(display_param->ifwificonnect == WIFI_CONNECT){
        grid6_temporary.grid6_bit_t.grid6_wifi = 1;
    }else{
        grid6_temporary.grid6_bit_t.grid6_wifi = 0;
    }
    
    /* 闪烁状态 索引 */
    // if(display_param->sys_status != SYS_RUN_NORMAL){
    switch(display_param->sys_pointer_index){
        case MODE_TIME:
            grid5_temporary.grid5_bit_t.grid5_point = (flash_status == FLASH_ON)?1:0;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_DLMS:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = (flash_status == FLASH_ON)?1:0;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_DLQD:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = (flash_status == FLASH_ON)?1:0;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_DLPL:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = (flash_status == FLASH_ON)?1:0;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_CCQD:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = (flash_status == FLASH_ON)?1:0;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_CZFD:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = (flash_status == FLASH_ON)?1:0;
            grid12_temporary.grid12_bit_t.grid12_czpl = 1;
            break;
        case MODE_CZPL:
            grid5_temporary.grid5_bit_t.grid5_point = 1;
            grid8_temporary.grid8_bit_t.grid8_dlms = 1;
            grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
            grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
            grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
            grid10_temporary.grid10_bit_t.grid10_czfd = 1;
            grid12_temporary.grid12_bit_t.grid12_czpl = (flash_status == FLASH_ON)?1:0;
            break;

        default:
            break;

    }
    // }else{
    //     grid5_temporary.grid5_bit_t.grid5_point = 1;
    //     grid8_temporary.grid8_bit_t.grid8_dlms = 1;
    //     grid10_temporary.grid10_bit_t.grid10_dlqd = 1;
    //     grid12_temporary.grid12_bit_t.grid12_dlpl = 1;
    //     grid8_temporary.grid8_bit_t.grid8_ccqd = 1;
    //     grid10_temporary.grid10_bit_t.grid10_czfd = 1;
    //     grid12_temporary.grid12_bit_t.grid12_czpl = 1;
    // }



    /* 声音 */
    switch(display_param->sys_vol_switch){
        case MUSIC_CLOSE:
            grid5_temporary.grid5_bit_t.grid5_mute = 1;
            grid5_temporary.grid5_bit_t.grid5_voice1 = 0;
            grid5_temporary.grid5_bit_t.grid5_voice2 = 0;
            grid5_temporary.grid5_bit_t.grid5_voice3 = 0;
            break;
        case MUSIC_VOICE1:
            grid5_temporary.grid5_bit_t.grid5_mute = 0;
            grid5_temporary.grid5_bit_t.grid5_voice1 = 1;
            grid5_temporary.grid5_bit_t.grid5_voice2 = 0;
            grid5_temporary.grid5_bit_t.grid5_voice3 = 0;
            break;
        case MUSIC_VOICE2:
            grid5_temporary.grid5_bit_t.grid5_mute = 0;
            grid5_temporary.grid5_bit_t.grid5_voice1 = 1;
            grid5_temporary.grid5_bit_t.grid5_voice2 = 1;
            grid5_temporary.grid5_bit_t.grid5_voice3 = 0;
            break;
        case MUSIC_VOICE3:
            grid5_temporary.grid5_bit_t.grid5_mute = 0;
            grid5_temporary.grid5_bit_t.grid5_voice1 = 1;
            grid5_temporary.grid5_bit_t.grid5_voice2 = 1;
            grid5_temporary.grid5_bit_t.grid5_voice3 = 1;
            break;
        default:
            break;           

    }

    /* 强度 */
    if(memcmp(data_strength_old,display_param->sys_mode_type_buffer,sizeof(data_strength_old)) != 0){
        switch (display_param->sys_mode_type_buffer[MODE_DLMS]){
            case MODE_DLMS_1:
                grid7_temporary.grid7_bit_t.grid7_dlms1 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms2 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms3 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms4 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms5 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms6 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms7 = 0;
                grid8_temporary.grid8_bit_t.grid8_dlms8 = 0;
                break;
            case MODE_DLMS_2:
                grid7_temporary.grid7_bit_t.grid7_dlms1 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms2 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms3 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms4 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms5 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms6 = 0;
                grid7_temporary.grid7_bit_t.grid7_dlms7 = 0;
                grid8_temporary.grid8_bit_t.grid8_dlms8 = 0;
                break;
            case MODE_DLMS_3:
                grid7_temporary.grid7_bit_t.grid7_dlms1 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms2 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms3 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms4 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms5 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms6 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms7 = 0;
                grid8_temporary.grid8_bit_t.grid8_dlms8 = 0;
                break;
            case MODE_DLMS_4:
                grid7_temporary.grid7_bit_t.grid7_dlms1 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms2 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms3 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms4 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms5 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms6 = 1;
                grid7_temporary.grid7_bit_t.grid7_dlms7 = 1;
                grid8_temporary.grid8_bit_t.grid8_dlms8 = 1;
                break;
            
            default:
                break;
        }
        switch (display_param->sys_mode_type_buffer[MODE_DLQD])
        {
            case POWER_0:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_1:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_2:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_3:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_4:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_5:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 0;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_6:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_7:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 1;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 0;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_8:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 1;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 1;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 0;
                break;
            case POWER_9:
                grid9_temporary.grid9_bit_t.grid9_dlqd1 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd2 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd3 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd4 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd5 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd6 = 1;
                grid9_temporary.grid9_bit_t.grid9_dlqd7 = 1;
                grid10_temporary.grid10_bit_t.grid10_dlqd8 = 1;
                grid10_temporary.grid10_bit_t.grid10_dlqd9 = 1;
                break;
            default:
                break;
        }
        switch (display_param->sys_mode_type_buffer[MODE_DLPL])
        {
            case POWER_0:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_1:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_2:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_3:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_4:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_5:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 0;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_6:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_7:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 1;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 0;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_8:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 1;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 1;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 0;
                break;
            case POWER_9:
                grid11_temporary.grid11_bit_t.grid11_dlpl1 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl2 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl3 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl4 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl5 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl6 = 1;
                grid11_temporary.grid11_bit_t.grid11_dlpl7 = 1;
                grid12_temporary.grid12_bit_t.grid12_dlpl8 = 1;
                grid12_temporary.grid12_bit_t.grid12_dlpl9 = 1;
                break;
            default:
                break;
        }
        switch (display_param->sys_mode_type_buffer[MODE_CCQD])
        {
            case POWER_0:
                grid8_temporary.grid8_bit_t.grid8_ccqd1 = 0;
                grid8_temporary.grid8_bit_t.grid8_ccqd2 = 0;
                grid8_temporary.grid8_bit_t.grid8_ccqd3 = 0;
                break;
            case POWER_1:
                grid8_temporary.grid8_bit_t.grid8_ccqd1 = 1;
                grid8_temporary.grid8_bit_t.grid8_ccqd2 = 0;
                grid8_temporary.grid8_bit_t.grid8_ccqd3 = 0;
                break;
            case POWER_2:
                grid8_temporary.grid8_bit_t.grid8_ccqd1 = 1;
                grid8_temporary.grid8_bit_t.grid8_ccqd2 = 1;
                grid8_temporary.grid8_bit_t.grid8_ccqd3 = 0;
                break;
            case POWER_3:
                grid8_temporary.grid8_bit_t.grid8_ccqd1 = 1;
                grid8_temporary.grid8_bit_t.grid8_ccqd2 = 1;
                grid8_temporary.grid8_bit_t.grid8_ccqd3 = 1;
                break;
            default:
                break;
        }
        switch (display_param->sys_mode_type_buffer[MODE_CZFD])
        {
            case POWER_0:
                grid10_temporary.grid10_bit_t.grid10_czfd1 = 0;
                grid10_temporary.grid10_bit_t.grid10_czfd2 = 0;
                grid10_temporary.grid10_bit_t.grid10_czfd3 = 0;
                break;
            case POWER_1:
                grid10_temporary.grid10_bit_t.grid10_czfd1 = 1;
                grid10_temporary.grid10_bit_t.grid10_czfd2 = 0;
                grid10_temporary.grid10_bit_t.grid10_czfd3 = 0;
                break;
            case POWER_2:
                grid10_temporary.grid10_bit_t.grid10_czfd1 = 1;
                grid10_temporary.grid10_bit_t.grid10_czfd2 = 1;
                grid10_temporary.grid10_bit_t.grid10_czfd3 = 0;
                break;
            case POWER_3:
                grid10_temporary.grid10_bit_t.grid10_czfd1 = 1;
                grid10_temporary.grid10_bit_t.grid10_czfd2 = 1;
                grid10_temporary.grid10_bit_t.grid10_czfd3 = 1;
                break;
            default:
                break;
        }
        switch (display_param->sys_mode_type_buffer[MODE_CZPL])
        {
            case POWER_0:
                grid12_temporary.grid12_bit_t.grid12_czpl1 = 0;
                grid12_temporary.grid12_bit_t.grid12_czpl2 = 0;
                grid12_temporary.grid12_bit_t.grid12_czpl3 = 0;
                break;
            case POWER_1:
                grid12_temporary.grid12_bit_t.grid12_czpl1 = 1;
                grid12_temporary.grid12_bit_t.grid12_czpl2 = 0;
                grid12_temporary.grid12_bit_t.grid12_czpl3 = 0;
                break;
            case POWER_2:
                grid12_temporary.grid12_bit_t.grid12_czpl1 = 1;
                grid12_temporary.grid12_bit_t.grid12_czpl2 = 1;
                grid12_temporary.grid12_bit_t.grid12_czpl3 = 0;
                break;
            case POWER_3:
                grid12_temporary.grid12_bit_t.grid12_czpl1 = 1;
                grid12_temporary.grid12_bit_t.grid12_czpl2 = 1;
                grid12_temporary.grid12_bit_t.grid12_czpl3 = 1;
                break;
            default:
                break;
        }
        memcpy(data_strength_old, display_param->sys_mode_type_buffer, sizeof(data_strength_old));
    }



    /* 倒计时标识 常量 */
    grid5_temporary.grid5_bit_t.grid5_countdown = 1;

    grid_buffer[GRID5] = grid5_temporary.grid5_byte;
    grid_buffer[GRID6] = grid6_temporary.grid6_byte;
    grid_buffer[GRID7] = grid7_temporary.grid7_byte;
    grid_buffer[GRID8] = grid8_temporary.grid8_byte;
    grid_buffer[GRID9] = grid9_temporary.grid9_byte;
    grid_buffer[GRID10] = grid10_temporary.grid10_byte;
    grid_buffer[GRID11] = grid11_temporary.grid11_byte;
    grid_buffer[GRID12] = grid12_temporary.grid12_byte;
    return grid_buffer;
}













